package com.example.mapexport.utils;

import android.util.Log;

import com.amap.api.services.core.PoiItemV2;
import com.amap.api.services.poisearch.PoiResultV2;
import com.amap.api.services.poisearch.PoiSearchV2;
import com.example.mapexport.realm.CitySearchResult;
import com.example.mapexport.realm.MyPhoto;
import com.example.mapexport.realm.MyPoiItem;
import com.example.mapexport.realm.PoiSearchResult;
import com.google.gson.Gson;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmResults;

public class RealmUtils {
    private static Realm realm;

    private static RealmUtils realmUtils;
    public static RealmUtils getInstance(){
        if(null == realmUtils){
            realmUtils = new RealmUtils();
            realm = Realm.getDefaultInstance();
        }
        return realmUtils;
    }

    public static void savePoiLocalData(PoiSearchV2.Query query, String city, PoiSearchV2.SearchBound searchBound, PoiResultV2 poiResultV2) {
        Gson gson = new Gson();
        // 添加数据
        realm.executeTransactionAsync(r -> {
            PoiSearchResult poiSearchResult = r.createObject(PoiSearchResult.class,System.currentTimeMillis());
            poiSearchResult.setCity(city);
            poiSearchResult.setQuery(gson.toJson(query));
            poiSearchResult.setSearchBound(gson.toJson(searchBound));

            List<PoiItemV2> result = poiResultV2.getPois();
            for (int i = 0;i < result.size();i++){
                MyPoiItem item = new MyPoiItem();
                item.setTitle(result.get(i).getTitle());
                if(null != result.get(i).getBusiness()) {
                    item.setTel(result.get(i).getBusiness().getTel());
                }
                RealmList<MyPhoto> photoList = new RealmList<>();
                for (int j = 0;j < result.get(i).getPhotos().size();j++){
                    MyPhoto photo = new MyPhoto();
                    photo.setTitle(result.get(i).getPhotos().get(j).getTitle());
                    photo.setUrl(result.get(i).getPhotos().get(j).getUrl());
                    photoList.add(photo);
                }
                item.setPhotos(photoList);
                poiSearchResult.getResult().add(item);
            }
        },() -> {
            Log.e("添加站点数据成功","1111");
        },error -> {
            Log.e("添加站点数据报错",error.toString());
        });
    }

    public static PoiSearchResult getPoiLocalData(PoiSearchV2.Query query,PoiSearchV2.SearchBound searchBound) {
        Gson gson = new Gson();
        // 查询数据
        PoiSearchResult poiSearchResult = realm.where(PoiSearchResult.class)
                .equalTo("query",gson.toJson(query))
                .equalTo("searchBound",gson.toJson(searchBound))
                .findFirst();
        return poiSearchResult;
    }

    public static void saveCityLocalData(String city,double lat,double lng) {
        // 添加数据
        realm.executeTransactionAsync(r -> {
            CitySearchResult citySearchResult = r.createObject(CitySearchResult.class,System.currentTimeMillis());
            citySearchResult.setCityName(city);
            citySearchResult.setLat(lat);
            citySearchResult.setLng(lng);
        },() -> {
            Log.e("添加经纬度数据成功","1111");
        },error -> {
            Log.e("添加经纬度数据报错",error.toString());
        });
    }

    public static CitySearchResult getCityLocalData(String cityName) {
        // 查询数据
        CitySearchResult citySearchResult = realm.where(CitySearchResult.class)
                .equalTo("cityName",cityName)
                .findFirst();
        return citySearchResult;
    }

    public static RealmResults<PoiSearchResult> getCityAllPoiData(String city,PoiSearchV2.Query query){
        Gson gson = new Gson();
        return realm.where(PoiSearchResult.class).equalTo("city", city).equalTo("query",gson.toJson(query)).findAll();
    }
}
